<?php 

class GamerCard
{
	public $URL;
	
	public $GamerTag;
	
	public $AccountStatus;
	
	public $Live_Xbox_Com_URL;
	
	public $Real_GamerTag;
	
	public $GamerScore;
	
	public $Zone;
	
	public $Games = array();
	
	public function __construct($gamertag)
	{
		if (!preg_match('/^[A-Za-z0-9 ]{1,16}$/',$gamertag))
		{
			throw new Exception('Invalid GamerTag');
		}
		
		$this->GamerTag = $gamertag;
		
		$url = 'http://gamercard.xbox.com/en-US/'.
			str_replace(' ','%20',$this->GamerTag).'.card';

		$this->URL = $url;
		
		try
		{
			$res = netHttp::quickGet($url);
		}
		catch (Exception $e)
		{
			throw new Exception($e);
		}

		preg_match('/<head>(.*?)<title>(.*?)<\/title>(.*?)<\/head>/msu',
			$res,$match);

		# Gamertag with correct capitalization
		$this->Real_GamerTag = $match[2];

		preg_match('/<body>(.*?)<\/body>/msu',$res,$data);
		
		$data = $data[1];
		
		preg_match('/<div class="XbcGamercard (Gold|Silver)/msu',
			$data,$match);
		
		$this->AccountStatus = $match[1];

		preg_match('/<a id="Gamertag" href="(.*?)">/msu',
			$data,$match);
		
		# get the profile URL
		$this->Live_Xbox_Com_URL = str_replace('%2B',' ',$match[1]);
		
		if (empty($this->Real_GamerTag))
		{
			throw new Exception('Empty GamerTag');
		}

		# GamerScore
		preg_match('/<div id="Gamerscore">(\d+)<\/div>/msu',$data,$match);
		
		$this->GamerScore = $match[1];
		
		$this->Zone = '';

		# Games
		preg_match('/<ol id="PlayedGames" >(.*?)<\/ol>/msu',$data,$games);
		
		$games = $games[1];

		$this->Games = array();
		$this->GamesWithImagesURL = array();
		
		if (preg_match_all('/src="(.*?)" alt="(.*?)"/msu',$games,$ms))
		{
			$i = 0;
			foreach($ms[2] as $game)
			{
				$this->Games[] = $game;
				$this->GamesWithImagesURL[$game] = $ms[1][$i];
				$i++;
			}
		}
	}
}

?>